import { getUser } from '@/api'
import { defineStore } from 'pinia'
import { reactive } from 'vue'

const useUser = defineStore(
  'user',
  () => {
    const defaultUser = {
      isLogin: false,
      username: '',
      avatar: '',
    }
    const user = reactive(Object.assign({}, defaultUser))
    const updateUser = async () => {
      const data = await getUser()
      if(data){
        user.isLogin = true
        user.username = data.username
        user.avatar = data.avatar
      }
      return user
    }
    const removeUser = () => {
      user.isLogin = false
      user.username = ''
      user.avatar = ''
      return user
    }

    return { user, updateUser, removeUser }
  },
  {
    persist: {  
      enabled: true,
      strategies: [
        {
          key: 'user',
          storage: localStorage,
        },
      ],
    },
  },
)
export default useUser
